import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFrameDemo2 {
    public static void main(String[] args) {
        JFrame jf = new JFrame("登录");
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(3);
        jf.setAlwaysOnTop(true);
        jf.setLocationRelativeTo(null);
        jf.setLayout(null);

        JLabel userName = new JLabel("用户名");
        userName.setBounds(50,50,50,20);
        jf.add(userName);

        JTextField textField = new JTextField();
        textField.setBounds(150,50,180,20);
        jf.add(textField);

        JLabel passWord = new JLabel("密码");
        passWord.setBounds(50,100,50,20);
        jf.add(passWord);

        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(150,100,180,20);
        jf.add(passwordField);

        JButton jButton = new JButton("登录");
        jButton.setBounds(50,200,280,20);
        jf.add(jButton);

        String user = "itheima";
        String password = "123456";

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a = textField.getText().trim();
                String b = passwordField.getText().trim();

                if(b.length() < 6 || b.length() > 12){
                    JOptionPane.showMessageDialog(jf,"密码长度为6-12位");
                    passwordField.setText("");
                    return;
                }

                if(a.equals(user) & b.equals(password)){
                    JOptionPane.showMessageDialog(jf,"登录成功");
                    passwordField.setText("");
                    textField.setText("");
                }else{
                    JOptionPane.showMessageDialog(jf,"用户名或密码错误");
                    passwordField.setText("");
                    textField.setText("");
                }
            }
        });

        jf.setVisible(true);

    }
}
